package com.qiyuan.essay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiyuan.common.core.mybatisplus.core.ServicePlusImpl;
import com.qiyuan.common.core.page.TableDataInfo;
import com.qiyuan.common.core.domain.PageQuery;
import com.qiyuan.common.utils.StringUtils;
import com.qiyuan.essay.domain.BlogArticle;
import com.qiyuan.essay.domain.bo.BlogArticleBo;
import com.qiyuan.essay.domain.vo.BlogArticleVo;
import com.qiyuan.essay.mapper.BlogArticleMapper;
import com.qiyuan.essay.service.IBlogArticleService;
import com.qiyuan.repository.ArticleRepository;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 文章信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-16
 */
@Service
public class BlogArticleServiceImpl extends ServicePlusImpl<BlogArticleMapper, BlogArticle, BlogArticleVo> implements IBlogArticleService {

    @Resource
    private ArticleRepository articleRepository;

    @Override
    public BlogArticleVo queryById(Long id){
        BlogArticleVo vo = getVoById(id);
        if (StringUtils.isNotBlank(vo.getArticleTag())){
            String tags [] = vo.getArticleTag().split(",");
            if (tags.length > 0){
                vo.setArticleTags(Arrays.asList(tags));
            }
        }else{
            vo.setArticleTags(new ArrayList<>());
        }
        return vo;
    }

    @Override
    public TableDataInfo<BlogArticleVo> queryPageList(BlogArticleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogArticle> lqw = buildQueryWrapper(bo);
        Page<BlogArticleVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<BlogArticleVo> queryList(BlogArticleBo bo) {
        LambdaQueryWrapper<BlogArticle> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<BlogArticle> buildQueryWrapper(BlogArticleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogArticle> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getArticleHeadPic()), BlogArticle::getArticleHeadPic, bo.getArticleHeadPic());
        lqw.like(StringUtils.isNotBlank(bo.getArticleName()), BlogArticle::getArticleName, bo.getArticleName());
        lqw.eq(StringUtils.isNotBlank(bo.getArticleRemark()), BlogArticle::getArticleRemark, bo.getArticleRemark());
        lqw.eq(bo.getArticleReadCount() != null, BlogArticle::getArticleReadCount, bo.getArticleReadCount());
        lqw.eq(bo.getArticleState() != null, BlogArticle::getArticleState, bo.getArticleState());
        lqw.eq(bo.getManagerId() != null, BlogArticle::getManagerId, bo.getManagerId());
        lqw.like(StringUtils.isNotBlank(bo.getManagerName()), BlogArticle::getManagerName, bo.getManagerName());
        lqw.eq(StringUtils.isNotBlank(bo.getArticleContent()), BlogArticle::getArticleContent, bo.getArticleContent());
        lqw.eq(bo.getArticleType() != null, BlogArticle::getArticleType, bo.getArticleType());
        lqw.eq(bo.getArticleStarNum() != null, BlogArticle::getArticleStarNum, bo.getArticleStarNum());
        lqw.eq(bo.getArticleConNum() != null, BlogArticle::getArticleConNum, bo.getArticleConNum());
        lqw.eq(StringUtils.isNotBlank(bo.getEnclosure()), BlogArticle::getEnclosure, bo.getEnclosure());
        lqw.eq(StringUtils.isNotBlank(bo.getGood()), BlogArticle::getGood, bo.getGood());
        lqw.eq(StringUtils.isNotBlank(bo.getRecommend()), BlogArticle::getRecommend, bo.getRecommend());
        lqw.eq(StringUtils.isNotBlank(bo.getOfficial()), BlogArticle::getOfficial, bo.getOfficial());
        lqw.eq(StringUtils.isNotBlank(bo.getTop()), BlogArticle::getTop, bo.getTop());
        lqw.eq(StringUtils.isNotBlank(bo.getBoutique()), BlogArticle::getBoutique, bo.getBoutique());
        return lqw;
    }

    @Override
    public Boolean insertByBo(BlogArticleBo bo) {
        BlogArticle add = BeanUtil.toBean(bo, BlogArticle.class);
        validEntityBeforeSave(add);
        add.setArticleTag(StringUtils.join(bo.getArticleTags(), ","));
        boolean flag = save(add);
        //存入ES
        articleRepository.save(add);
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(BlogArticleBo bo) {
        BlogArticle update = BeanUtil.toBean(bo, BlogArticle.class);
        if (CollectionUtil.isNotEmpty(bo.getArticleTags())){
            String result = bo.getArticleTags().stream().map(String::valueOf).collect(Collectors.joining(","));
            update.setArticleTag(result);
        }
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BlogArticle entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        boolean flag = false;
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        List<BlogArticle> blogArticleList = listByIds(ids);
        if (CollectionUtil.isNotEmpty(blogArticleList)){
            for (BlogArticle article : blogArticleList){
                article.setArticleState(1l);
                flag = updateById(article);
            }
        }
        articleRepository.deleteAll(blogArticleList);
//        return removeByIds(ids);
        return flag;
    }

    @Override
    public Boolean updateSx(Collection<Long> ids, String bs,Boolean isValid) {
        boolean flag = false;
        List<BlogArticle> blogArticleList = listByIds(ids);
        if (CollectionUtil.isNotEmpty(blogArticleList)){
            for (BlogArticle article : blogArticleList){
                if ("topRecommend".equals(bs)){
                    if ("1".equals(article.getTopRecommend())){
                        article.setTopRecommend("0");
                    }else{
                        article.setTopRecommend("1");
                    }
                }
                if ("good".equals(bs)){
                    if ("1".equals(article.getGood())){
                        article.setGood("0");
                    }else{
                        article.setGood("1");
                    }
                }
                if ("recommend".equals(bs)){
                    if ("1".equals(article.getRecommend())){
                        article.setRecommend("0");
                    }else{
                        article.setRecommend("1");
                    }
                }
                if ("official".equals(bs)){
                    if ("1".equals(article.getOfficial())){
                        article.setOfficial("0");
                    }else{
                        article.setOfficial("1");
                    }
                }
                if ("top".equals(bs)){
                    if ("1".equals(article.getTop())){
                        article.setTop("0");
                    }else{
                        article.setTop("1");
                    }
                }
                if ("boutique".equals(bs)){
                    if ("1".equals(article.getBoutique())){
                        article.setBoutique("0");
                    }else{
                        article.setBoutique("1");
                    }
                }
                article.setUpdateTime(new Date());
                flag = updateById(article);
            }
        }
        return flag;
    }
}
